//
//  CListViewController.m
//  ElectronicMall
//
//  Created by langapple on 15-2-8.
//  Copyright (c) 2015年 cczu. All rights reserved.
//

#import "CListViewController.h"
#import "EMSearchBar.h"
#import "EMSegmentedControl.h"
#import "EMNewContentCell.h"
#import "AppDelegate.h"
#import "MJRefresh.h"
@interface CListViewController ()
{
    UITextField * tf100;
    BOOL flagg;
    EMSegmentedControl *segment;
    AppDelegate * app;
}
@end

@implementation CListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    flagg = YES;
    app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    EMSearchBar *search = [[EMSearchBar alloc] initWithFrame:CGRectMake(0, 0, 140, 24)];
    search.placeholderText = @"点击搜索商品";
    for (UIView *tf in search.subviews) {
        if ([tf isKindOfClass:[UITextField class]]) {
            tf100 = (UITextField *)tf;
        }
    }
    tf100.delegate = self;
    self.navigationItem.titleView = search;
    
    UIBarButtonItem *logoImage = [[UIBarButtonItem alloc] init];
    UIImage *imageQ = [UIImage imageNamed:@"button_导航_返回_绿"];
    UIImageView *leftView = [[UIImageView alloc] initWithImage:[imageQ imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    leftView.frame = CGRectMake(0, 0, 23, 23);
    logoImage.customView = leftView;
    self.navigationItem.leftBarButtonItem = logoImage;
    
     segment = [[EMSegmentedControl alloc] initWithFrame:CGRectMake(0, 64, 320, 30)];
    [segment setTitleArray:@[@"默认▼", @"销量∧",@"价格∧", @"时间∧"]];
    
    [self.view addSubview:segment];
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64+30, __MainScreen_Width, __MainScreen_Height-94-49) collectionViewLayout:flowLayout];
    [collectionView registerClass:[EMNewContentCell class] forCellWithReuseIdentifier:@"EMNewContentCell"];
    collectionView.backgroundColor = RGB(236, 236, 236);
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    self.view.backgroundColor = RGB(236, 236, 236);
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
    [segment setActionBlock:^(NSUInteger index) {
        switch (index) {
            case 0:
                [self getRightCellListInfo:@"1"];
                break;
            case 1:
                if(flagg){
                    [self getRightCellListInfo:@"2"];
                }else{
                    [self getRightCellListInfo:@"3"];
                }
                flagg = !flagg;
                break;
            case 2:
                if(flagg){
                    [self getRightCellListInfo:@"4"];
                }else{
                    [self getRightCellListInfo:@"5"];
                }
                flagg = !flagg;
                break;
            case 3:
                if(flagg){
                    [self getRightCellListInfo:@"6"];
                }else{
                    [self getRightCellListInfo:@"7"];
                }
                flagg = !flagg;
                break;
            default:
                break;
        }
    }];
}


- (void)getRightCellListInfo:(NSString * )str
{
    [MBProgressHUD  showHUDAddedTo:self.navigationController.view animated:YES];
    EMGoodListReqBody *reqBody = [[EMGoodListReqBody alloc] init];
    reqBody.JUDGEMETHOD = @"GOODS-MENU-LIST";
    reqBody.ID = @"1";
    reqBody.GETTIMES = @"1";
    reqBody.FLAG = str;
    HessianFormDataRequest *request = [[HessianFormDataRequest alloc] init];
    // 模型转字典
    request.postData = reqBody.keyValues;
    [request startRequestWithURL:baseURL andCompletionBlcok:^(NSDictionary *respInfo) {
        // 将返回的字典转成模型，键的名称必须相同
        EMGoodListRespBody *respBody = [EMGoodListRespBody objectWithKeyValues:respInfo];
        [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
        NSLog(@"%@", respInfo);
        if ([respBody.ERRORCODE isEqualToString: @"0000"])
        {
            NSMutableArray * leftArr = [[NSMutableArray alloc]init];
            leftArr = [respBody.SUCCESSFULINFO mutableCopy];
            NSEnumerator *enumJson = [leftArr objectEnumerator];
            NSDictionary *dic;
            while ((dic = enumJson.nextObject))
            {
//                [leftItemArr addObject:
//                 @[[StringUtil strForDicKey:dic andKey:@"id"],
//                   [StringUtil strForDicKey:dic andKey:@"name"]]];
            }
//            NSLog(@"%@",rightItemArr);
        }
        else
        {
            [app showTip:respInfo[@"ERRORDESTRIPTION"]];
        }
        
    } andFailedBlock:^{
        
        [app showTip:@"无法连接服务器、请稍后再试"];
    }];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
   return 20;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"EMNewContentCell";
    EMNewContentCell *cell = (EMNewContentCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    return cell;
    
    
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    UIEdgeInsets top = {5, 10, 5, 10};
    return top;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(145, 245);
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [app showAlert:@"123"];
}

@end
